package client

import (
	nacoscfg "flare-admin/config/nacos"
	"flare-admin/proto"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

var (
	gpuMetricClient proto.GPUMetricsServiceClient
)

func init() {
	var err error
	gpuMetricClient, err = NewGPUMetricGrpcClient(nacoscfg.ApplicationCfg.Grpc.GpuMetric.EndPoint)
	if err != nil {
		panic("Failed to create gRPC client: " + err.Error())
	}
}

func NewGPUMetricGrpcClient(endpoint string) (proto.GPUMetricsServiceClient, error) {
	conn, err := grpc.NewClient(endpoint, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		return nil, err
	}
	clt := proto.NewGPUMetricsServiceClient(conn)
	return clt, nil
}

func GetGpuMetricClient() proto.GPUMetricsServiceClient {
	if gpuMetricClient == nil {
		panic("gRPC client is not initialized")
	}
	return gpuMetricClient
}
